package com.wokeMy.view.channe.yibao;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.method.MyApp;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.channe.rongbangfd.RbRecordCreditActivity;
import com.wokeMy.view.channe.rongbangjf.RbjfKtxyActivity;
import com.wokeMy.view.channe.rongbangjf.RbjfktxyHtmlActivity;
import com.wokeMy.view.channe.rongbangjf.RbqrHtmlActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class YbSelectActivity extends BaseActivity implements View.OnClickListener {

    TextView zfcard_rbselect,jscard_rbselect;
    TextView money_rbselect_et;
    RelativeLayout zhifu_rbselect_rl;
    RelativeLayout jieshuan_rbselect_rl;
    ImageView back;
    TextView rbselect_text_title;
    Button paybt;

    Data_bindcarlist listdata,payListdata;
    Intent intent;
    float money;
    int way_id,vip_level;
    String user_id;
    String pay_card_no,card_no;
    Dialog mydialog;

    Double yueMoney;
    String status="0",treatycode,smsseq,authcode,ordercode;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_rb_select);
    }

    @Override
    public void initActivity() {
        zhifu_rbselect_rl.setOnClickListener(this);
        jieshuan_rbselect_rl.setOnClickListener(this);
        back.setOnClickListener(this);
        rbselect_text_title.setOnClickListener(this);
        paybt.setOnClickListener(this);
        money_rbselect_et.setText(money+" 元");
        mydialog=Util.createLoadingDialog(YbSelectActivity.this);
        ybisRz();
    }

    @Override
    public void initView() {
        money_rbselect_et = myFindViewById(R.id.money_rbselect_et);
        rbselect_text_title = myFindViewById(R.id.rbselect_text_title);

        zhifu_rbselect_rl = myFindViewById(R.id.zhifu_rbselect_rl);
        jieshuan_rbselect_rl = myFindViewById(R.id.jieshuan_rbselect_rl);
        zfcard_rbselect = myFindViewById(R.id.zfcard_rbselect);
        jscard_rbselect = myFindViewById(R.id.jscard_rbselect);
        back = myFindViewById(R.id.back_rbselect_iv);
        paybt = myFindViewById(R.id.pay_rbselect_bt);


    }

    @Override
    public void initData() {
        intent=getIntent();
        way_id = intent.getIntExtra ("way_id", 0);
        money = intent.getFloatExtra ("money", 0.01f);
        //        user_id = intent.getStringExtra("user_id");
        yueMoney=0.00;
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        vip_level=(Integer.parseInt(((MyApp)getApplication()).getDatas_load().getUser_type()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_rbselect_iv:
                finish();
                break;
            case R.id.rbselect_text_title:
                Intent inte = new Intent (YbSelectActivity.this, YbRecordActivity.class);
                inte.putExtra ("way_id",way_id);
                inte.putExtra ("yb","yb");
                startActivity(inte);
                break;
            case R.id.pay_rbselect_bt:
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(YbSelectActivity.this,"付款未选择",Toast.LENGTH_SHORT).show();
                } else if(card_no==null||card_no.equals("")){
                    Toast.makeText(YbSelectActivity.this,"收款卡未选择",Toast.LENGTH_SHORT).show();
                } else {
                    mydialog=  Util.createLoadingDialog(YbSelectActivity.this);
                    rbyuZf();

                }
                break;
            case R.id.zhifu_rbselect_rl: // 付款卡
                    Intent intent = new Intent (YbSelectActivity.this, MybankblindActivity.class);
                    intent.putExtra ("czhiortxian", "zhifucard");
                    startActivityForResult (intent, Constant.REQUEST_ZHIFU);
                break;
            case R.id.jieshuan_rbselect_rl: // 收款卡
                String aa=jscard_rbselect.getText().toString().trim();

                if (TextUtils.isEmpty(aa)){
                    Intent intent2 = new Intent (YbSelectActivity.this, MybankblindActivity.class);
                    intent2.putExtra ("czhiortxian", "zscard");
                    startActivityForResult (intent2, Constant.REQUEST_JIESHUAN);

                }else {
                    tishi("此通道只能进件一次");
                }
//                Intent intent2 = new Intent (YbSelectActivity.this, MybankblindActivity.class);
//                intent2.putExtra ("czhiortxian", "zscard");
//                startActivityForResult (intent2, Constant.REQUEST_JIESHUAN);
                break;
            default:
                break;
        }
    }



    //易宝判断商户是否入驻
    private void  ybisRz() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        Log.e("易宝判断商户是否入驻url",Constant.YBISRZ_URL);
        Log.e("易宝判断商户是否入驻params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.YBISRZ_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        //                        Toast.makeText(YbSelectActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("易宝判断商户是否入驻Result", result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            Util.closeDialog(mydialog);
                            if (jsonObject.get("flag").equals("success")) {
                                JSONObject jsonObject1 = jsonObject.getJSONObject("data").getJSONObject("data");
                                status = jsonObject1.getString("status");
                                if (status.equals("1")){
                                    card_no=jsonObject1.getString("accno");
                                    jscard_rbselect.setText(Util.bankCard(card_no));
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(mydialog);
            }
        });
    }
    //易宝订单支付
    private void rbyuZf() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("bankaccount",pay_card_no); // 支付卡号（信用卡）
        params.put("amount",money); // 最低：10元（测试）
        Log.e("易宝订单支付url",Constant.YBZF_URL);
        Log.e("易宝订单支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.YBZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        //                        Toast.makeText(YbSelectActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("易宝订单支付Result", result);
                        try {
                            Util.closeDialog(mydialog);
                            JSONObject jsonObject = new JSONObject(result);
                            String info = jsonObject.getString("info");
                            if (jsonObject.get("flag").equals("success")) {
                                if (jsonObject.has("data")){
                                    String string = jsonObject.getJSONObject("data").getString("data");
                                    Intent intent=new Intent(YbSelectActivity.this,YbHtmlActivity.class);
                                    intent.putExtra("html",string);
                                    startActivity(intent);
                                }

                            } else {
                                Util.closeDialog(mydialog);
                                if (jsonObject.has("data")){
                                    info=jsonObject.getJSONObject("data").getJSONObject("data").getString("message");
                                }
                                tishi(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(mydialog);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //        Log.e("RESPONSE_JIESHUAN",resultCode+"");
        if (resultCode==Constant.RESPONSE_ZHIFU){
            payListdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=payListdata.cardno;
            zfcard_rbselect.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        }else if(resultCode==Constant.RESPONSE_JIESHUAN){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            card_no=listdata.cardno;
//            jscard_rbselect.setText(card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+card_no.substring(card_no.length()-3,card_no.length()));
            Intent intent=new Intent(YbSelectActivity.this,YbJingjianActivity.class);
            intent.putExtra("card_no", card_no);
            startActivityForResult(intent,Constant.REQUEST_RHKJZF);//
        } else if(resultCode==Constant.RESPONSE_RHLZ){

        } else if(resultCode==Constant.RESPONSE_RHKJZF){
            String no=data.getStringExtra("card_no");
            jscard_rbselect.setText(no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+no.substring(no.length()-3,no.length()));
        }
    }

}
